const { excludePaths } = require('./excludePaths.cjs')
var fs = require('fs')
var path = require('path')
/**目标文件夹 */
// let filePath = path.join(__dirname, '../src/pages/fixLicense')
let filePath = path.join(__dirname, '../src')
var logZhSubPath = path.join(__dirname, '../i18n/locales/zhSub.json')
var logZhJsonPath = path.join(__dirname, '../i18n/locales/zh.json')

var regFiles = /(.tsx)|(.ts)/ //需要处理的文件后缀
var regNotes = /(\*\/)|(\/\/)|(\/\*)|(\*)/g //注释
// var regNotes = /(\*\/)|(\/\/)|(\/\*)/g //需要处理的文件后缀
// var regHtml= /(<(S*?)[`^>]*>.*?</>)|(<.*?/>) /
var dictionary = (function () {
	var map = {}
	var mapSub = {}
	return {
		set: function (key, val) {
			map[key] = val || ''
		},
		setSub: function (key, val) {
			mapSub[key] = val || ''
		},
		get: function (key) {
			return map[key] || ''
		},
		save2File: function () {
			fs.writeFile(
				this.logZhSubPath,
				JSON.stringify(map).replace(/","/g, '",\r\n"'),
				{ encoding: 'utf8', flag: 'w' },
				function (err) {
					if (err) throw err
				}
			)
		},
		save2FileSub: function () {
			fs.writeFile(
				this.logZhJsonPath,
				JSON.stringify(mapSub).replace(/","/g, '",\r\n"'),
				{ encoding: 'utf8', flag: 'w' },
				function (err) {
					if (err) throw err
				}
			)
		},
		loadFile: function (callback) {
			fs.readFile(this.logZhSubPath, { encoding: 'utf8' }, function (err, data) {
				map = JSON.parse(data)
				callback()
			})
		},
	}
})()
// 遍历查找文件
const File = function () {
	var index = 0
	var _readFile = function (pathStr, fileBack, doneBack) {
		//异步的读取文件的内容
		fs.readFile(pathStr, { encoding: 'utf8' }, function (err, data) {
			index--
			if (err) {
				data = ''
				console.log(err, pathStr)
				//throw err;
			}
			fileBack(data, pathStr)
			if (index === 0) {
				doneBack()
			}
		})
	}
	var _walkDir = function (pathStr, fileBack, doneBack) {
		//获得指定目录下，所有文件的列表
		fs.readdir(pathStr, function (err, files) {
			// console.log(pathStr, 'pathStr--------')
			// console.log(files, 'files--------')
			files.forEach(function (file) {
				var currentFilePath = path.join(pathStr, file)
				// console.log(currentFilePath,'currentFilePath')
				/**是否T当前文件，默认T */
				let isTCurrentFile = true
				if (excludePaths.length) {
					for (var i = 0; i < excludePaths.length; i++) {
						// console.log(excludePaths[i],'---------------kkk')
						if (currentFilePath.indexOf(excludePaths[i]) !== -1) {
							isTCurrentFile = false
							break
						}
						// if (excludePaths[i] === currentFilePath) {
						// 	isTCurrentFile = false
						// 	break
						// }
					}
				}
				if (isTCurrentFile) {
					console.log(pathStr, '11111111pathStr')
					//是否是一个文件夹
					if (fs.statSync(pathStr + '/' + file).isDirectory()) {
						_walkDir(pathStr + '/' + file, fileBack, doneBack)
					} else {
						if (regFiles.test(file)) {
							index++
							_readFile(pathStr + '/' + file, fileBack, doneBack)
						}
						return
					}
				}
			})
		})
	}
	this.walkDir = function (pathStr, fileBack, doneBack) {
		index = 0
		_walkDir(pathStr, fileBack, doneBack)
	}
}
dictionary.logZhJsonPath = logZhJsonPath
dictionary.logZhSubPath = logZhSubPath
exports.File = File
exports.dictionary = dictionary
exports.dictionary = dictionary
exports.filePath = filePath
exports.regNotes = regNotes
